package com.th.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.Service.OrderService;
import com.th.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author
 */
@Controller
@RequestMapping("/authc")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/orderlist")
    public String list(Model model, @RequestParam(value = "pagenum", defaultValue = "1") String pageNum) {
        //查询全部
        IPage<Order> page = orderService.page(new Page<>(Integer.parseInt(pageNum), 10));
        model.addAttribute("page", page);
        return "authc/order";
    }

    @PostMapping("/orderadd")
    @ResponseBody
    public String orderAdd(Long customer, String detail, Double paidAmount, String adress){
        Order order = new Order();
        order.setCustomerId(customer);
        order.setDetail(detail);
        order.setPaidAmount(paidAmount);
        order.setAdress(adress);
        order.setCreateTime(new Date());
        orderService.save(order);
        return "1";
    }

    @GetMapping("/orderdelete")
    @ResponseBody
    public String delete(String id) {
        Boolean result = orderService.removeById(Integer.parseInt(id));
        if (result) {
            return "1";
        }
        return "0";
    }
}
